package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;


import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.NumberUtils;

/**
 * 青海新闻网 解析类
 * 
 * @author Chase
 * 
 */
public class QhNewsAnalyse implements AnalyseNews {

	public static void main(String[] args) {
		QhNewsAnalyse parser=new QhNewsAnalyse();
//		String Url="http://www.qhnews.com/newscenter/system/2012/11/22/010959696.shtml";
		String Url="http://www.qhnews.com/newscenter/system/2014/09/12/011505627.shtml";
		UrlMeta oneMeta=CrawlHTML.responseToURL(Url);
		System.out.println(parser.isDetailPage(Url));
		System.out.println(parser.parserHtml(oneMeta));
	}

	
	public boolean isDetailPage(String url) {
		boolean bRet=false;
		//http://www.qhnews.com/zwpd/system/2011/03/29/010324107.shtml
		String[] regex={
				"http://www.qhnews.com/.*/system/[0-9]{4}/[0-9]{2}/[0-9]{2}/[0-9]*.shtml"
				};
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {

		String url=urlMeta.getUrl(); 
		/**
		 * 判断是否为详细页，不是详细页面直接返回空
		 */
		if (!isDetailPage(url)) {
		}

		String html=urlMeta.getHtml();
		if(html==null){
		}		
		/*
		 * 定义新闻实体需要的字段
		 */
		String title=null;
		String content=null;
		Long dateLong=null;
		String commentNum=null;
		String clickNum=null;

		Document doc=Jsoup.parse(html);
		title=doc.select("span.lan14_h").text();// 标题
		if(title==null || title.trim().length()==0){
			title=doc.select("h1").text();
		}
		if(title==null || title.trim().length()==0){
		}

		dateLong=DateUtils.matchDate(doc.select("table.navy").text());// 时间
		if(dateLong==null || dateLong==0){
			dateLong=DateUtils.matchDate(doc.select("div.addtime").text());
		}
		if(dateLong==null){
			dateLong=JSoupUtils.matchDate(doc, "发布时间：");
		}
		content=HtmlCleaner.getContentHtml(url,doc.select("div#zoom")); //内容
		
//	commentNum=doc.select("em#comNum").text(); //评论数
		
		
		NewsMeta meta=new NewsMeta();
		meta.setTitle(title);
		meta.setUrl(url);
		meta.setType(1);
		meta.setClickNum(NumberUtils.parseInt(clickNum));
		meta.setCommentNum(NumberUtils.parseInt(commentNum));
		meta.setContent(content);
		meta.setDate(dateLong);

		return meta;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
